package com.huitone.gddw.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


public class ContextPropertiesUtils extends PropertyPlaceholderConfigurer {

	private static Map<String,String> propertyMap;
	
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		propertyMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = StringUtils.objToString(key.toString()).trim();
            String value = StringUtils.objToString(props.getProperty(keyStr)).trim();
            propertyMap.put(keyStr, value);
        }
	}

	public static String getProperty(String name) {
		
		if (propertyMap == null) return null;
		
        return propertyMap.get(name);
    }
	
	public static List<String> getPropertys(String regex) {
		
		if (propertyMap == null) return null;
		
		List<String> resultList = new ArrayList<>();
		
		List<String> keyList = new ArrayList<>();
		
		Iterator<String> iterator = propertyMap.keySet().iterator();
		
		while(iterator.hasNext()) {
			String next = iterator.next();
			if (Pattern.compile(regex).matcher(next).matches()) {
				keyList.add(next);
			}
		}
		
		Collections.sort(keyList);
		
		for (String key:keyList) {
			resultList.add(propertyMap.get(key));
		}
		
		return resultList;
	}
	
}
